<?php

	class cct_order extends ctl_order
	{
		function exportXls()
		{
			include_once('shopObject.php');
			if ($_POST['order_id'] == '')
			{
				$where = '1';
			}
			else
			{
				$order_id = '('.implode(',',$_POST['order_id']).')';
				$where = 'A.order_id in '.$order_id;
			}
			$io = 'xls';
			$objectName = 'order';
			$this->db = $this->system->database();
			$dataio = $this->system->loadModel('system/dataio');
			//检查是否有非会员顾客订单
			$check_memeber_sql = "SELECT COUNT(order_id) AS total from sdb_orders AS A where member_id IS NULL AND {$where}";
			$check_order = $this->db->selectrow($check_memeber_sql);

			//会员购物订单
			$sSql = "SELECT A.order_id,B.bn,B.name,B.price,B.nums,B.amount,from_unixtime(A.createtime),from_unixtime(A.acttime),
						A.ship_name,A.ship_tel,A.ship_area,A.ship_addr,A.ship_zip,A.mark_text,A.shipping,
						A.cost_freight,D.custom_name,C.uname,A.pay_status,A.ship_status
					FROM sdb_orders AS A, sdb_order_items AS B, sdb_members AS C, sdb_payment_cfg AS D
					WHERE A.order_id = B.order_id AND C.member_id = A.member_id	AND D.id = A.payment
							AND A.disabled = 'false' AND D.disabled = 'false' AND C.disabled = 'false' AND {$where}";
			$aData = $this->db->select($sSql);

			//若非会员顾客订单不为空,合并2个订单列表数组
			if ($check_order['total'])
			{
				//非会员顾客订单
				$sql = "SELECT A.order_id,B.bn,B.name,B.price,B.nums,B.amount,from_unixtime(A.createtime),from_unixtime(A.acttime),			A.ship_name,A.ship_tel,A.ship_area,A.ship_addr,A.ship_zip,A.mark_text,A.shipping,
								A.cost_freight,D.custom_name,A.member_id as uname,A.pay_status,A.ship_status
						FROM sdb_orders AS A, sdb_order_items AS B,sdb_payment_cfg AS D
						WHERE A.order_id = B.order_id AND D.id = A.payment
							AND A.disabled = 'false' AND D.disabled = 'false' AND A.member_id is NULL AND {$where}";
				$null_order  = $this->db->select($sql);
				$aData = array_merge($aData,$null_order);
			}
			$count = count($aData);
			for ($i=0; $i<=$count-1; $i++)
			{
				if ($aData[$i]['uname'] == NULL)
				{
					$aData[$i]['uname'] = "非会员顾客";
				}
				switch ($aData[$i]['pay_status'])
				{
					case 0:
						//unset($aData[$i]['pay_status']);
						$aData[$i]['pay_status'] = '未支付';
						break;
					case 1:
						//unset($aData[$i]['pay_status']);
						$aData[$i]['pay_status'] = '已支付';
						break;
					case 2:
						//unset($aData[$i]['pay_status']);
						$aData[$i]['pay_status'] = '处理中';
						break;
					case 3:
						//unset($aData[$i]['pay_status']);
						$aData[$i]['pay_status'] = '部分付款';
						break;
					case 4:
						//unset($aData[$i]['pay_status']);
						$aData[$i]['pay_status'] = '部分退款';
						break;
					case 5:
						//unset($aData[$i]['pay_status']);
						$aData[$i]['pay_status'] = '全额退款';
						break;
				}
				switch ($aData[$i]['ship_status'])
				{
					case 0:
						//unset($aData[$i]['ship_status']);
						$aData[$i]['ship_status'] = '未发货';
						break;
					case 1:
						//unset($aData[$i]['ship_status']);
						$aData[$i]['ship_status'] = '已发货';
						break;
					case 2:
						//unset($aData[$i]['ship_status']);
						$aData[$i]['ship_status'] = '部分发货';
						break;
					case 3:
						//unset($aData[$i]['ship_status']);
						$aData[$i]['ship_status'] = '部分退货';
						break;
					case 4:
						//unset($aData[$i]['ship_status']);
						$aData[$i]['ship_status'] = '已退货';
						break;
				}
			}

			$aInfo = array(
				'订单号'=>'order_id',
				'商品货号'=>'bn',
				'商品名称'=>'name',
				'单价'=>'price',
				'数量'=>'nums',
				'商品总价'=>'amount',
				'下单日期'=>'createtime',
				'最后更新'=>'acttime',
				//'订单总额'=>'total_amount',
				'收货人'=>'ship_name',
				'收货人电话'=>'ship_tel',
				'收货地区'=>'ship_area',
				'收货地址'=>'ship_addr',
				'邮编'=>'zip',
				'订单备注'=>'mark_text',
				//'出库时间'=>'',
				'配送方式'=>'shipping',
				'配送费用'=>'cost_freight',
				'支付方式'=>'payment',
				'会员用户名'=>'uname',
				'付款状态'=>'pay_status',
				'发货状态'=>'ship_status'
				//'货款是否到帐'=>''
			);
			$headCols = array();
			foreach ($aInfo as $key => $var)
			{
				$headCols[] = $key.'('.$var.')';
			}
			$dataio->export_begin($io,$headCols,$objectName,$count);
			$dataio->export_rows($io,$aData);
			$dataio->export_finish($io);
		}
	}
?>